/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.dt.core.initializer;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.spi.MetadataContentManager;
import com.inspur.edp.sgf.api.entity.SgMetadata;
import com.inspur.edp.sgf.dt.core.exception.ExceptionCode;
import com.inspur.edp.sgf.utils.exception.EApiDesignException;
import io.iec.edp.caf.businessobject.api.entity.DevBasicBoInfo;
import io.iec.edp.caf.businessobject.api.service.DevBasicInfoService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * 功能描述:
 *
 * @ClassName: EapiInitializer
 * @Author: Fynn Qi
 * @Date: 2020/12/24 14:54
 * @Version: V1.0
 */
public class EapiInitializer implements MetadataContentManager {

    @Override
    public void build(GspMetadata metadata) {
        SgMetadata eapi = new SgMetadata();
        eapi.setId(metadata.getHeader().getId());
        eapi.setCode(metadata.getHeader().getCode());
        eapi.setName(metadata.getHeader().getName());
        eapi.setVersion("1.0");
        eapi.getService().setId(UUID.randomUUID().toString());
        eapi.setBizObject(metadata.getHeader().getBizobjectID());
        DevBasicInfoService devBasicInfoService = SpringBeanUtils.getBean(DevBasicInfoService.class);
        DevBasicBoInfo devBasicBoInfo = devBasicInfoService.getDevBasicBoInfo(metadata.getHeader().getBizobjectID());
        if (devBasicBoInfo != null) {
            eapi.setCriticalApplication(devBasicBoInfo.getAppCode());
            eapi.setServiceUnit(devBasicBoInfo.getSuCode());
        } else {
            throw new EApiDesignException(ExceptionCode.GSP_EAPI_DESIGN_0001,
                    "Eapi异常：根据业务对象ID：" + metadata.getHeader().getBizobjectID() + "获取业务对象信息失败，请检查业务对象ID是否被修改、与当前是否一致");
        }
        if (devBasicBoInfo.getBoInfo() == null || StringUtils.isBlank(devBasicBoInfo.getBoInfo().getCode())) {
            throw new EApiDesignException(ExceptionCode.GSP_EAPI_DESIGN_0002,
                    "Eapi异常：根据业务对象ID:" + metadata.getHeader().getBizobjectID() + "获取业务对象编号失败，请检查业务对象ID是否被修改、与当前是否一致");
        }
        //TODO 后续修改routerUrl
        eapi.setRouteUrl(eapi.getCode().toLowerCase());
        //eapi.setRouteUrl(String.format("%s_%s",devBasicBoInfo.getBoInfo().getCode().toLowerCase(),eapi.getCode().toLowerCase()));
        metadata.setContent(eapi);
    }
}
